package controller

import (
	"errors"
	"fmt"
	"ordersystem/config"
	"ordersystem/model"
	"ordersystem/util"
	"strconv"
	"time"

	"github.com/gofiber/fiber/v2"
)

func LabQueryLabController(ctx *fiber.Ctx) error {
	var lab model.Lab
	ctx.BodyParser(&lab)
	fmt.Println(lab)

	detailLab, er := lab.QueryDetailLab()

	if er != nil {
		return er
	}
	devices, err := model.QueryDevicesByLabId(int(lab.Id))

	if err != nil {
		return err
	}

	result := make(map[string]interface{})

	result["lab"] = detailLab
	result["devices"] = devices
	fmt.Println(result)
	util.SendResponse(ctx, result)
	return nil
}

func LabQueryLabsController(ctx *fiber.Ctx) error {
	var page PageParam
	ctx.BodyParser(&page)

	result := util.GetCookieForMap(ctx)

	var params int32
	if result["identify"] == 0 {
		params = 0
	} else {
		params = result["id"].(int32)
	}

	results, err := model.QueryLabsForAdminOrSuperAdmin(page.Page-1, params)
	if err != nil {
		return err
	}

	util.SendResponse(ctx, results)
	return nil
}

func LabUpdateLabController(ctx *fiber.Ctx) error {
	var lab model.Lab
	ctx.BodyParser(&lab)

	if ctx.Cookies("identify") == "0" {
		if err := lab.UpdateLab(); err != nil {
			return err
		}
		util.SendResponse(ctx, nil)
		return nil
	}

	if ctx.Cookies("identify") == "2" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	var originLab model.Lab
	originLab.Id = lab.Id

	if err := originLab.QueryLab(); err != nil {
		return err
	}

	cookie := util.GetCookieForMap(ctx)

	if originLab.Creator != cookie["id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := lab.UpdateLab(); err != nil {
		return err
	}
	util.SendResponse(ctx, nil)

	return nil
}

func LabQueryTagsController(ctx *fiber.Ctx) error {
	result := model.LabQueryTag()
	fmt.Println(result)
	util.SendResponse(ctx, result)
	return nil
}

type QueryLabDataPackage struct {
	LabId int32     `json:"lab_id"`
	Tag   string    `json:"tag"`
	Date  time.Time `json:"date"`
}

func LabQueryByLabIdAndDateController(ctx *fiber.Ctx) error {
	var requestData QueryLabDataPackage
	ctx.BodyParser(&requestData)
	var result interface{}
	fmt.Println("requestData", requestData)
	result, err := model.QueryByLabIdAndDate(requestData.LabId, requestData.Date)

	if err != nil {
		return err
	}

	util.SendResponse(ctx, result)
	return nil
}

func LabQueryByTagAndDateController(ctx *fiber.Ctx) error {
	var requestData QueryLabDataPackage
	ctx.BodyParser(&requestData)

	var result interface{}
	result, err := model.QueryByTagAndDate(requestData.Tag, requestData.Date)

	if err != nil {
		return err
	}

	util.SendResponse(ctx, result)
	return nil
}

func LabDeleteLabController(ctx *fiber.Ctx) error {
	var lab model.Lab
	ctx.BodyParser(&lab)

	// Delete the Lab from the superadmin or himself!

	if ctx.Cookies("identify") == "0" {
		if err := lab.DeleteLab(); err != nil {
			return err
		}
		util.SendResponse(ctx, nil)
		return nil
	}

	// Don't permit Lab to delete other Lab or admin or superadmin!

	if ctx.Cookies("identify") == "2" {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	// I need to check origin Lab data to see if the Lab is createBy this admin

	var originLab model.Lab
	originLab.Id = lab.Id

	if err := originLab.QueryLab(); err != nil {
		return err
	}

	cookie := util.GetCookieForMap(ctx)

	if originLab.Creator != cookie["id"] {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := lab.DeleteLab(); err != nil {
		return err
	}
	util.SendResponse(ctx, nil)

	return nil
}

func LabCreateLabController(ctx *fiber.Ctx) error {
	var lab model.Lab
	ctx.BodyParser(&lab)
	data, _ := strconv.Atoi(ctx.Cookies("id"))
	lab.Creator = int32(data)

	data, _ = strconv.Atoi(ctx.Cookies("identify"))

	if data == 2 {
		return errors.New(config.REQUEST_UNAUTHORIZATION_ERROR)
	}

	if err := lab.CreateLab(); err != nil {
		return err
	}

	util.SendResponse(ctx, lab.Id)

	return nil
}

func LabQueryNumberController(ctx *fiber.Ctx) error {

	result := util.GetCookieForMap(ctx)

	var params int32
	if result["identify"] == 0 {
		params = 0
	} else {
		params = result["id"].(int32)
	}
	if number, err := model.QueryLabNumber(params); err != nil {
		return err
	} else {
		util.SendResponse(ctx, number)
		return nil
	}
}
